import 'package:flutter/material.dart';

class LeftDialog extends StatefulWidget {
  final Animation<double> parentAnimation;
  final Widget child;

  LeftDialog(this.parentAnimation, this.child);

  @override
  _LeftDialogState createState() => _LeftDialogState();
}

class _LeftDialogState extends State<LeftDialog> {
  late Animation<double> animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    var curveTween = CurveTween(curve: Curves.easeOutQuint);
    animation = curveTween.animate(widget.parentAnimation);
    widget.parentAnimation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        var curveTween = CurveTween(curve: Curves.easeInQuart);
        animation = curveTween.animate(widget.parentAnimation);
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: GestureDetector(
          onTap: () {
            Navigator.of(context).pop();
          },
          child: FadeTransition(
            opacity: animation,
            child: Container(
                color: Color(0x66111111),
                alignment: Alignment.centerLeft,
                child: SizeTransition(
                    sizeFactor: animation,
                    axis: Axis.horizontal,
                    axisAlignment: 0.0,
                    child: GestureDetector(onTap: () {}, child: widget.child))),
          )),
    );
  }
}


class BottomDialog extends StatefulWidget {
  final Animation<double> parentAnimation;
  final Widget child;

  BottomDialog(this.parentAnimation, this.child);

  @override
  _BottomDialogState createState() => _BottomDialogState();
}

class _BottomDialogState extends State<BottomDialog> {
  late Animation<double> animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    var curveTween = CurveTween(curve: Curves.easeOutQuint);
    animation = curveTween.animate(widget.parentAnimation);
    widget.parentAnimation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        var curveTween = CurveTween(curve: Curves.easeInQuart);
        animation = curveTween.animate(widget.parentAnimation);
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: GestureDetector(
          onTap: () {
            Navigator.of(context).pop();
          },
          child: FadeTransition(
            opacity: animation,
            child: Container(
                color: Color(0x66111111),
                alignment: Alignment.bottomCenter,
                child: SizeTransition(
                    sizeFactor: animation,
                    axis: Axis.vertical,
                    axisAlignment: 0.0,
                    child: GestureDetector(onTap: () {}, child: widget.child))),
          )),
    );
  }
}


class AlphaDialog extends StatefulWidget {
  final Animation<double> parentAnimation;
  final Widget child;

  AlphaDialog(this.parentAnimation, this.child);

  @override
  _AlphaDialogState createState() => _AlphaDialogState();
}

class _AlphaDialogState extends State<AlphaDialog> {
  late Animation<double> animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    var curveTween = CurveTween(curve: Curves.easeOutQuint);
    animation = curveTween.animate(widget.parentAnimation);
    widget.parentAnimation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        var curveTween = CurveTween(curve: Curves.easeInQuart);
        animation = curveTween.animate(widget.parentAnimation);
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: GestureDetector(
          onTap: () {
            Navigator.of(context).pop();
          },
          child: FadeTransition(
            opacity: animation,
            child: Container(
                color: Colors.transparent,
                alignment: Alignment.centerLeft,
                child: GestureDetector(onTap: () {}, child: widget.child)),
          )),
    );
  }
}

class RightDialog extends StatefulWidget {
  final Animation<double> parentAnimation;
  final Widget child;

  RightDialog(this.parentAnimation, this.child);

  @override
  _RightDialogState createState() => _RightDialogState();
}

class _RightDialogState extends State<RightDialog> {
  late Animation<double> animation;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    var curveTween = CurveTween(curve: Curves.easeOutQuint);
    animation = curveTween.animate(widget.parentAnimation);
    widget.parentAnimation.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        var curveTween = CurveTween(curve: Curves.easeInQuart);
        animation = curveTween.animate(widget.parentAnimation);
        setState(() {});
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: GestureDetector(
          onTap: () {
            Navigator.of(context).pop();
          },
          child: FadeTransition(
            opacity: animation,
            child: Container(
                color: Color(0x66111111),
                alignment: Alignment.centerRight,
                child: SizeTransition(
                    sizeFactor: animation,
                    axis: Axis.horizontal,
                    axisAlignment: 0.0,
                    child: GestureDetector(onTap: () {}, child: widget.child))),
          )),
    );
  }
}

class CenterDialog extends StatefulWidget {
  final Animation<double> parentAnimation;
  final Widget child;
  final Function? onDispose;

  CenterDialog(this.parentAnimation,this.child, {this.onDispose});

  @override
  _CenterDialogState createState() => _CenterDialogState();
}

class _CenterDialogState extends State<CenterDialog> {
  late Animation<double> animation;

  @override
  void initState() {
    super.initState();

    var curveTween = CurveTween(curve: Curves.easeOutQuint);
    animation = curveTween.animate(widget.parentAnimation);
    widget.parentAnimation.addStatusListener((status) {
      // if (status == AnimationStatus.completed) {
      //   var curveTween = CurveTween(curve: Curves.easeInQuart);
      //   animation = curveTween.animate(widget.parentAnimation);
      //   setState(() {});
      // }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    if(widget.onDispose!=null) {
      widget.onDispose!();
    }
  }



  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: GestureDetector(
          onTap: () {
            Navigator.of(context).pop();
          },
          child:Container(
            color: Color(0x01ffffff),
              alignment: Alignment.center,
              child: ScaleTransition(
                  scale: animation,
                  child: GestureDetector(onTap: () {}, child: widget.child)))),
    );
  }
}



class ToastDialog extends StatefulWidget {
  final Animation<double> parentAnimation;
  final Widget child;
  final Function? onDispose;
  final Color? backgroundColor;

  ToastDialog(this.parentAnimation,this.child, {this.onDispose,this.backgroundColor});

  @override
  _ToastDialogState createState() => _ToastDialogState();
}

class _ToastDialogState extends State<ToastDialog> {
  late Animation<double> animation;
  late Animation<double> tweenAnim;

  var curveTween = CurveTween(curve: Curves.fastOutSlowIn);
  var tween =Tween<double>(begin:1.6,end:1.0 );
  @override
  void initState() {
    super.initState();
    // var tween=

    animation = curveTween.animate(widget.parentAnimation);
    tweenAnim = tween.animate(animation);
    widget.parentAnimation.addStatusListener((status) {
      if(status==AnimationStatus.reverse){
        print("parent:${status}");


        tween=Tween<double>(begin:1.0,end:1.0 );
        // var curveTween = CurveTween(curve: Curves.easeOut);
        animation = curveTween.animate(widget.parentAnimation);
        tweenAnim = tween.animate(animation);
        setState(() {

        });
        widget.onDispose!();
      }
      if (status == AnimationStatus.completed) {
        tween=Tween<double>(begin:1.0,end:1.0 );
        // var curveTween = CurveTween(curve: Curves.easeOut);
        animation = curveTween.animate(widget.parentAnimation);
        tweenAnim = tween.animate(animation);
        setState(() {});
      }
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    // if(widget.onDispose!=null) {
    //   widget.onDispose();
    // }
  }



  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: GestureDetector(
          onTap: () {
            Navigator.of(context).pop();
          },
          child:Container(
            padding: EdgeInsets.only(left: 30, right: 30),
            child:  FadeTransition(
            opacity: animation,
            child: Container(
                color: Color(0x00000000),
                alignment: Alignment.centerRight,
                child: ScaleTransition(
                    scale: tweenAnim,
                    child: GestureDetector(onTap: () {}, child: widget.child))),
          ),)),
    );
  }
}
